# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Maintainer: Eric Bélanger <eric@archlinux.org>

pkgbase=jasper
pkgname=('jasper' 'jasper-doc')
pkgver=4.0.0
pkgrel=1
pkgdesc='Software-based implementation of the codec specified in the emerging JPEG-2000 Part-1 standard'
url='https://www.ece.uvic.ca/~frodo/jasper/'
arch=('x86_64')
license=('custom:JasPer2.0')
makedepends=('glibc' 'libjpeg' 'freeglut' 'libxmu' 'glu' 'cmake' 'doxygen')
source=(${pkgname}-${pkgver}.tar.gz::https://github.com/mdadams/jasper/archive/version-${pkgver}.tar.gz
        jasper-1.900.1-fix-filename-buffer-overflow.patch)
sha512sums=('dc03434870e85b1fdbdf41a3e07ce5a8044c8d225717d3d7a5969d45e3bd716bfb2abc4b4e0b8cec2f4071a7520c5d3e48939dba0cabe6d192eacd593de0c563'
            'b8d798bf75523c5db263783e42c653dd0cb03deee90be32eddf878bb6893cca02abadd94de6a8c737a5b7fe76f7fb245979f010765e6a95fc520b215e3a2a7f0')
b2sums=('d4d8ae3324f3753e999adee57c215b89fa0ddeb3d945f10de15459a7d6566f1de9cbcd27a418bcc3c128bc138fff7e3f1cc157b6750d2d7dc1216f8e8cbc8bea'
        '9b1927a437ed9b32491f07a210600b5a7d10711ec954dadc030a8238b67e9c6d1a8e67956fd7a5515e7060f0a1651fa86763e21d6d20592f8f00e9d3260722a1')

prepare() {
  cd ${pkgbase}-version-${pkgver}
  patch -p1 < "${srcdir}/jasper-1.900.1-fix-filename-buffer-overflow.patch"
  sed -r 's|(CMAKE_SKIP_BUILD_RPATH) FALSE|\1 TRUE|g' -i CMakeLists.txt
}

build() {
  cd ${pkgbase}-version-${pkgver}

  cmake \
    -B buildx \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_C_FLAGS="$CFLAGS -ffat-lto-objects" \
    -DJAS_ENABLE_OPENGL=ON \
    -DJAS_ENABLE_LIBJPEG=ON \
    -DJAS_ENABLE_AUTOMATIC_DEPENDENCIES=OFF \
    -DCMAKE_SKIP_RPATH=ON \
    -DJAS_ENABLE_SHARED=ON
  cmake --build buildx
}

check() {
  cd ${pkgbase}-version-${pkgver}
  export LD_LIBRARY_PATH="$(pwd)/buildx/src/libjasper/"
  make -C buildx -j1 test
}

package_jasper() {
  depends=('glibc' 'libjpeg' 'libjpeg.so' 'freeglut' 'glu')
  optdepends=('jasper-doc: documentation')
  provides=('libjasper.so')

  cd ${pkgbase}-version-${pkgver}
  make -C buildx DESTDIR="${pkgdir}" install
  rm -r "${pkgdir}/usr/share/doc"

  install -Dm 644 NEWS.txt README.md  -t "${pkgdir}/usr/share/doc/${pkgname}"
  install -Dm 644 LICENSE.txt  -t "${pkgdir}/usr/share/licenses/${pkgname}"
}

package_jasper-doc() {
  pkgdesc+=' (documentation)'
  optdepends=('jasper')

  cd ${pkgbase}-version-${pkgver}
  make -C buildx DESTDIR="${pkgdir}" install
  rm -rf "${pkgdir}/usr/"{bin,include,lib,share/man}

  install -Dm 644 LICENSE.txt -t "${pkgdir}/usr/share/licenses/${pkgname}"
}

# vim: ts=2 sw=2 et:
